<template>
  <div>
    <van-nav-bar
      title="注册"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
      type="primary"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,8}$/, message: '用户名至少包含5-8个字符' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{5,12}$/, message: '用户名至少包含5-12个字符' },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
    <router-link class="link" to="/denglu">登录账号</router-link>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { registered } from '@/api/user'
// import { setToken } from '@/utils/storage'
// import request from '@/utils/request'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    // 返回按钮
    onClickLeft () {
      Toast('返回')
    },
    async onSubmit (values) {
      console.log(values) // 打印账号密码
      // 获取token命令
      await registered(values)
      // console.log(res) // 打印token
      // setToken(res.body.token) // 本地存储
      // this.$toast.success('少年你打开了新世界的大门') // 窗口提示
      // this.$router.push('/denglu') // 跳转页面
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
